import Cookies from 'js-cookie'
import storage from 'store2'

const app = {
  state: {
    site: {},
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'en',
    equType:'', // 设备类型
  },
  getters:{
    equTypeName:state=>{
      switch (state.equType) {
        case "qzj":
          return "起重机械";
        case "dqkg":
          return "电气控制柜";
        case "yycg":
          return "储罐";
        case "jtt":
          return "焦炭塔";
        case "dzgl":
          return "锅炉";
        case "ydqp":
          return "气瓶";
        case "ahct":
          return '氨合成塔'
        default:
          return '设备'
      }
    }
  },
  mutations: {
    SET_SITE: (state, site) => {
      state.site = site
    },
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    SET_EQUTYPE:(state,equType)=>{ // 切换设备类型
      state.equType=equType;
      storage.set('equType',equType)
    }
  },
  actions: {
    site({ commit }, site) {
      return new Promise((resolve, reject) => {
        commit('SET_SITE', site)
        resolve(site)
      })
    },
    toggleSideBar({ commit }) {
      commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    setEquType({commit}){
      // 从localstorage里获取设备类型
      const equType=storage.get('equType');
      if(equType){
        commit('SET_EQUTYPE',equType);
      }else{
        commit('SET_EQUTYPE','qzj')
      }
    }
  }
}

export default app
